package com.zzb.projectfactory.framework.utils;

import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.filter.ByteArrayComparable;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * hbase utils
 * Created by chenpi on 2016/11/17.
 */
public class HbaseUtils {

    public static SimpleDateFormat timestampFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

    /**
     * 获取 qualifier 值
     * @param result result
     * @param qualifier qualifier
     * @return String
     */
    public static String getValue(Result result, String qualifier,String family) {
        byte[] bytes = result.getValue(Bytes.toBytes(family), Bytes.toBytes(qualifier));
        if(bytes==null)
            return null;
        return Bytes.toString(bytes);
    }

    /**
     * 获得long值
     * @param result result
     * @param qualifier qualifier
     * @return
     */
    public static long getLongValue(Result result, String qualifier,String family, long defaultValue) {
        byte[] bytes = result.getValue(Bytes.toBytes(family), Bytes.toBytes(qualifier));
        if(bytes==null){
            return defaultValue;
        }
        return Bytes.toLong(bytes);
    }

    /**
     * 添加字段
     * @param put put
     * @param qualifier qualifier
     * @param value value
     */
    public static void addColumn(Put put, String family,String qualifier, byte[] value){

        byte[] familyBytes = Bytes.toBytes( family);
        byte[] qualifierBytes = Bytes.toBytes(qualifier);
        put.add(familyBytes,qualifierBytes,value);
    }

    /**
     * 创建字段值过滤器
     * @param qualifier qualifier
     * @param value value
     * @return
     */
    public static SingleColumnValueFilter createColumnValueFilter(String qualifier,String family, String value){
        return createColumnValueFilter(qualifier,family, CompareFilter.CompareOp.EQUAL,value);
    }

    /**
     * 创建字段值过滤器
     * @param qualifier
     * @param compareOp
     * @param value
     * @return
     */
    public static SingleColumnValueFilter createColumnValueFilter(String qualifier,String family, CompareFilter.CompareOp compareOp, String value){
        byte[] familyBytes = Bytes.toBytes(family);
        byte[] qualifierBytes = Bytes.toBytes(qualifier);
        byte[] valueBytes = Bytes.toBytes(value);
        return new SingleColumnValueFilter(familyBytes, qualifierBytes, compareOp, valueBytes);
    }

    /**
     * 创建字段值过滤器
     * @param qualifier
     * @param compareOp
     * @param comparator
     * @return
     */
    public static SingleColumnValueFilter createColumnValueFilter(String qualifier,String family, CompareFilter.CompareOp compareOp, ByteArrayComparable comparator){
        byte[] familyBytes = Bytes.toBytes(family);
        byte[] qualifierBytes = Bytes.toBytes(qualifier);
        return new SingleColumnValueFilter(familyBytes, qualifierBytes, compareOp, comparator);
    }

    /**
     * 把日期转换成时间戳字符串
     */
    public static String toTimestampString(Date date){
        return timestampFormat.format(date);
    }

    /**
     * 把时间戳字符串转换成日期类型
     */
    public static Date toTimestamp(String timestampString) throws ParseException {
        return timestampFormat.parse(timestampString);
    }

    /**将存入hbase的sql恢复空格*/
    public static String recoveryBlank(String withBlankPlacementString){
        if(withBlankPlacementString == null) return null;
        Pattern spritPattern = Pattern.compile("\\\\ ");
        Pattern equalPattern = Pattern.compile("\\\\=");
        Pattern commaPattern = Pattern.compile("\\\\,");

        return commaPattern.matcher(equalPattern.matcher(spritPattern.matcher(withBlankPlacementString).replaceAll(" ")).replaceAll("=")).replaceAll(",");
    }

    public static String replaceToFullIp(String ip){
        ip = ip.trim();
        String result = ip;
        Matcher matcher = Pattern.compile("^(\\d{1,3}).(\\d{1,3}).(\\d{1,3}).(\\d{1,3})$").matcher(ip);
        if(matcher.find()){
            int i = matcher.groupCount();
            result = String.format("%03d.%03d.%03d.%03d",Integer.parseInt(matcher.group(1)),Integer.parseInt(matcher.group(2)),Integer.parseInt(matcher.group(3)),Integer.parseInt(matcher.group(4)));
        }
        return result;
    }
    public static String replaceToTrimIp(String ip){
        ip = ip.trim();
        String result = ip;
        Matcher matcher = Pattern.compile("^(\\d{1,3}).(\\d{1,3}).(\\d{1,3}).(\\d{1,3})$").matcher(ip);
        if(matcher.find()){
            int i = matcher.groupCount();
            result = String.format("%d.%d.%d.%d",Integer.parseInt(matcher.group(1)),Integer.parseInt(matcher.group(2)),Integer.parseInt(matcher.group(3)),Integer.parseInt(matcher.group(4)));
        }
        return result;
    }

    public static void main(String[] args) {
        String ip ="127.0.0.1";
        System.out.println(replaceToFullIp(ip));
        String fullIp = "127.000.000.001";
        System.out.println(replaceToTrimIp(fullIp));
    }
}
